package com.threeSales.services;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.threeSales.dao.UserDao;
import com.threeSales.dao.UserNexusDao;
import com.threeSales.entity.Admin;
import com.threeSales.entity.User;
import com.threeSales.entity.UserNexus;


@Service
public class UserService {
	
	@Autowired
	private UserDao userDao;
	
	@Autowired
	private UserNexusService userNexusService;
	
//	@Transactional(rollbackFor=Exception.class)
/*	@Transactional
	public String findUser(int name,String pwd){
		String result = "";
			try {
				//测试是否运行
				//userDao.testadd();
				User user = userDao.findUser(name, pwd);
				result="登陆成功";
			} catch (Exception e) {
				result="用户名或密码错误";
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();  
			}
			
			return result;
		
	}*/
	
	public String findAdmin(int name,String pwd){
		String result = "";
		try{
			userDao.findAdmin(name, pwd);
			result = "登陆成功";
		}catch(Exception e){
			result = "用户名或密码错误";
		}
		return result;
	}
	
	public Admin loginFindAdmin(int name,String pwd){
		try {
			return userDao.findAdmin(name, pwd);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	@Transactional
	public String userAdd(User user,String id){
		String result = "";
		try{
			UserNexus userNexus = new UserNexus(id,id,"0");
			userNexusService.aotuAdd(userNexus);
			userDao.addUser(user);
			result = "会员添加成功";
		}catch(Exception e){
			result = "会员添加失败";
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();  
		}
		return result;
		
	}
	
	public List findUser(int name,String pwd){
		List list = new ArrayList();
		String result = "";
			try {
			    User user = userDao.findUser(name, pwd);
				result="登陆成功";
				list.add(result);
				list.add(user);
			} catch (Exception e) {
				result="用户名或密码错误";
				list.add(result);
			}
			return list;
	}
	
	@Transactional
	public String addUser(User user,String fuid,String id){
		String result = "";
		try{
			UserNexus userNexus = new UserNexus(id,id,"0");
			userNexusService.aotuAdd(userNexus);
			userDao.addUser(user);
			User userFirst = userDao.findUser(fuid);
			UserNexus userNexusFirst = new UserNexus(fuid,id,"1");
			userNexusService.nexusAdd(userNexusFirst);
			User userSecond = userDao.findUser(userFirst.getFid());
			if(userSecond != null){
				UserNexus userNexusSecond = new UserNexus(userSecond.getId(),id,"2");
				userNexusService.nexusAdd(userNexusSecond);
				User userThird = userDao.findUser(userSecond.getFid());
				if(userThird != null){
					UserNexus userNexusThird = new UserNexus(userThird.getId(),id,"3");
					userNexusService.nexusAdd(userNexusThird);
				}
			}
			result = "会员添成功";
		}catch(Exception e){
			result = "会员添加失败";
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();  
		}
		return result;
		
	}

	
	
}
